<template>
  <div class="app-container bg-gray">
    <el-card class="box-1">
      <div slot="header">
        <span>{{ title }}</span>
      </div>
      <el-form ref="form" :model="form" label-width="160px">
        <el-form-item label="UP主名称：">
          <el-input v-model="form.name"/>
        </el-form-item>
        <el-form-item label="头像：">
          <ws-upload-single-img v-model="form['avatarId']" :path="form['avatarUrl']"/>
        </el-form-item>
        <el-form-item label="管理员：">
          <el-select v-model="form.userId" placeholder="管理员" filterable multiple>
            <el-option
              v-for="item in userList"
              :key="item.id"
              :label="`${item.nickname}(${item.mobile})`"
              :value="item.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="简介：">
          <el-input v-model="form.desc" type="textarea" rows="4"/>
        </el-form-item>
        <div style="text-align:center">
          <el-button type="primary" @click="submitForm('form')">保存</el-button>
          <el-button type="primary" @click="$router.back(-1)">取消</el-button>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 页面标题
      title: '',

      form: {
        userId: []
      },
      // 允许发送短信剩余秒数
      smsSendSecond: 0,
      errMsg: {
        phone: '',
        code: ''
      },
      // 用户列表
      userList: []
    }
  },
  created() {
    this.title = this.$route.query.title
    if (this.$route.query.id) {
      this.id = this.$route.query.id
      this.getData(this.$route.query.id)
    }
    this.getUserList();
  },
  methods: {
    getData() {
      const url = '../videoAdmin/broadcaster/getInfo'
      const data = {id: this.id}
      this.$store.dispatch('GetConnect', { url, data }).then(res => {
        this.form = res.data
      }).catch(e => {
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    // 获取用户列表
    getUserList() {
      const url = '../videoAdmin/user/getListBySetUp'
      const data = {id: this.id}
      this.$store.dispatch('GetConnect', { url, data }).then(res => {
        this.userList = res.data
      }).catch(e => {
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    // 发送短信验证码
    sendSmsCode() {
      (async () => {
        const result = await this.$service.webSendSms({
          mobile: this.form.phone,
          flag: this.step === 1 ? 'CHANGEMOBILE' : 'VERIFY'
        });
        if (result.code === 0) {
          this.smsSendSecond = 60;
          const interval = setInterval(() => {
            this.smsSendSecond--;
            if (this.smsSendSecond <= 0) {
              this.smsSendSecond = 0;
              clearInterval(interval);
            }
          }, 1000);
        } else {
          this.errMsg.phone = result.msg;
        }
      })();
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const data = Object.assign({}, this.form);
          data.userId = data.userId.join(',');
          const url = this.id ? '../videoAdmin/broadcaster/updateInfo' : '../videoAdmin/broadcaster/addData'
          this.$store.dispatch('GetConnect', { url, data }).then(res => {
            this.$message.success(res.msg)
            this.$router.back(-1)
          }).catch((err) => {
            this.$message.error(err.msg)
          })
        } else {
          this.$message.error('提交失败,请检查必填项')
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="scss" type="text/scss" scoped>
@import '../../../../styles/index.scss';
.box-1 {
  max-width: 1000px;
  margin: 0 auto;
  .el-form {
    max-width: 900px;
    .title {
      font-weight: 600;
    }
    .remark {
      font-size: 13px;
      color: $textGray;
    }
    > div > .el-button {
      @extend .bg-green;
      width: 20%;
      min-width: 160px;
    }
  }
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
